<template>
  <BasicModal
    :footer="null"
    title="手写签批"
    v-bind="$attrs"
    :class="prefixCls"
    :width="630"
    @register="register"
  >
    <div class="h-260px flex flex-col">
      <div :id="`wp_${fieldName}`" class="container">
        <div
          style="background-size: cover"
          class="js-signature"
          data-line-color="#01018b"
          data-background="transparent url()"
        ></div>
      </div>
      <div :class="`${prefixCls}__footer mt-2`">
        <a-button type="primary" size="middle" @click="handleClear"> 清除 </a-button>
        <a-button type="primary" size="middle" class="ml-2" @click="handleOk"> 确定 </a-button>
        <a-button type="primary" size="middle" class="ml-2" @click="handleCancel"> 取消 </a-button>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { BasicModal, useModalInner } from '/@/components/Modal/index';
  export default defineComponent({
    name: 'WritePadModal',
    components: { BasicModal },
    emits: ['register', 'success'],
    setup(_, { emit }) {
      const { t } = useI18n();
      const { prefixCls } = useDesign('header-lock-modal');

      const width = ref(600);
      const height = ref(200);
      const w = ref(200);
      const h = ref(100);
      const fieldName = ref('');
      const [register, { closeModal }] = useModalInner(async (data) => {
        if (data) {
          if (data.width) {
            width.value = data.width;
          }
          if (data.w) {
            w.value = data.w;
          }
          fieldName.value = data.fieldName;
        }

        setTimeout(() => {
          if ($('#wp_' + fieldName.value + ' .js-signature').length) {
            var _width = width.value;
            var _height = height.value;
            var rate = 0;
            if (screen.width < _width) {
              rate = _width / screen.width;
              _width = _width / rate - 15;
              _height = _height / rate;
            }
            $('#wp_' + fieldName.value + ' .js-signature').jqSignature({
              autoFit: false,
              color: '#01018b',
              // background-color: '#fff',
              lineWidth: 3,
              width: _width,
              height: _height,
            });
          }
        }, 100);
      });

      async function handleOk() {
        var dataUrl = $('#wp_' + fieldName.value + ' .js-signature').jqSignature('getDataURL');
        var $img = $('<img>').attr('src', dataUrl);
        $img.css({ width: w.value + 'px', height: h.value + 'px' });

        console.log('handleOk dataUrl', dataUrl);
        setInputObjValue(dataUrl);
        $(findObj('pad_' + fieldName.value)).html($img.prop('outerHTML'));

        $('#wp_' + fieldName.value + ' .js-signature').jqSignature('clearCanvas');
        closeModal();
        emit('success', {});
      }

      async function handleCancel() {
        $('#wp_' + fieldName.value + ' .js-signature').jqSignature('clearCanvas');
        closeModal();
      }

      async function handleClear() {
        $('#wp_' + fieldName.value + ' .js-signature').jqSignature('clearCanvas');
      }

      return {
        t,
        prefixCls,
        register,
        handleOk,
        handleCancel,
        width,
        height,
        handleClear,
        fieldName,
      };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-header-lock-modal';

  .@{prefix-cls} {
    &__entry {
      position: relative;
      //height: 240px;
      padding: 130px 30px 30px;
      border-radius: 10px;
    }

    &__header {
      position: absolute;
      top: 0;
      left: calc(50% - 45px);
      width: auto;
      text-align: center;

      &-img {
        width: 70px;
        border-radius: 50%;
      }

      &-name {
        margin-top: 5px;
      }
    }

    &__footer {
      text-align: center;
    }
  }
</style>
